package gui;

import gui.keyActions.AnimatePolygonalsAction;
import gui.keyActions.DecrementSpeedAction;
import gui.keyActions.DeletingPointAction;
import gui.keyActions.IncrementCurveAction;
import gui.keyActions.IncrementSpeedAction;
import gui.keyActions.SetAllControlPointsVisibility;
import gui.keyActions.SetAllCurvesVisibilityAction;
import gui.keyActions.SetAllPolygonalsVisibilityAction;
import gui.keyActions.SetAllSecondaryPolygonalsVisibility;
import gui.netbeans.ButtonPanel;

import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class MainPanel extends JPanel {
	private DrawPanel drawPanel;
	private ButtonPanel buttonPanel;

	public MainPanel(DrawPanel drawPanel, ButtonPanel buttonPanel) {
		super();
		this.drawPanel = drawPanel;
		this.buttonPanel = buttonPanel;
		
		this.add(this.drawPanel);
		this.add(this.buttonPanel);

		// Key bindings
		this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("N"), "increment curve index");
		this.getActionMap().put("increment curve index", new IncrementCurveAction(this.drawPanel));

		this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("Q"), "hide all control points");
		this.getActionMap().put("hide all control points", new SetAllControlPointsVisibility(this));

		this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "hide all polygonals");
		this.getActionMap().put("hide all polygonals", new SetAllPolygonalsVisibilityAction(this));
		
		this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("E"), "hide all secondary polygonals");
		this.getActionMap().put("hide all secondary polygonals", new SetAllSecondaryPolygonalsVisibility(this));
		
		this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("R"), "hide all curves");
		this.getActionMap().put("hide all curves", new SetAllCurvesVisibilityAction(this));
		
		this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("A"), "animate polygonals");
		this.getActionMap().put("animate polygonals", new AnimatePolygonalsAction(this.drawPanel.getDraw()));

		this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"), "faster animation speed");
		this.getActionMap().put("faster animation speed", new IncrementSpeedAction(this));

		this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("D"), "slower animate polygonals");
		this.getActionMap().put("slower animate polygonals", new DecrementSpeedAction(this));

		this.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("C"), "delete a point");
		this.getActionMap().put("delete a point", new DeletingPointAction(this.drawPanel, this.drawPanel.getMouse()));

	}

	public DrawPanel getDrawPanel() {
		return drawPanel;
	}

	public void setDrawPanel(DrawPanel drawPanel) {
		this.drawPanel = drawPanel;
	}

	public ButtonPanel getButtonPanel() {
		return buttonPanel;
	}

	public void setButtonPanel(ButtonPanel buttonPanel) {
		this.buttonPanel = buttonPanel;
	}

	public void animationStep() {
		
		if (this.drawPanel.getDraw().isAnimatedPolygonals()){
			this.buttonPanel.getSliderFatorT().setEnabled(false);
			double t = this.drawPanel.animationStep();
			this.buttonPanel.getSliderFatorT().setValue((int) (t * this.buttonPanel.getSliderFatorT().getMaximum()));
		} else {
			this.buttonPanel.getSliderFatorT().setEnabled(true);
		}
	}

}
